﻿using MobileControlGuru.AutoTask;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Util;
using System.Windows.Forms;

namespace MobileControlGuru.Src
{
    public class Schedule
    {
        public TimeSpan TotalScheduleTime { get; set; }
        //= TimeSpan.FromHours(3); // 从12:00到15:00
        public int deviceWorkTimeInSeconds = 30;  // 新增设备工作时间参数
        public DateTime StartTime;
       
        public Schedule()
        {

        }





        public static List<ScheduleDevice> AssignNewDevicesRandomlyWithinInterval(List<ScheduleDevice> assignedDevices, List<ScheduleDevice> newDevices, DateTime startOfInterval, DateTime endOfInterval, Random random, int deviceWorkTimeInSeconds = 30)
        {
            List<ScheduleDevice> assignedNewDevices = new List<ScheduleDevice>();

            foreach (var newDevice in newDevices)
            {
                bool foundValidSlot = false;

                for (int attempt = 0; attempt < 1000; attempt++)
                {
                    TimeSpan randomTimeSpan = TimeSpan.FromSeconds(random.Next((int)(endOfInterval - startOfInterval).TotalSeconds));
                    DateTime startTime = startOfInterval + randomTimeSpan;
                    DateTime endTime = startTime + TimeSpan.FromSeconds(deviceWorkTimeInSeconds);

                    if (!HasConflict(assignedDevices, newDevice.Name, startTime, endTime) && startTime >= startOfInterval && endTime <= endOfInterval)
                    {
                        // 创建一个新的ScheduleDevice实例以避免修改原始newDevices列表
                        ScheduleDevice assignedDevice = new ScheduleDevice
                        {
                            Name = newDevice.Name,
                            WorkContent = newDevice.WorkContent, // 如果需要，也可以包含其他属性
                            StartTime = startTime,
                            EndTime = endTime
                        };

                        assignedDevices.Add(assignedDevice);
                        assignedNewDevices.Add(assignedDevice);
                        foundValidSlot = true;
                        break;
                    }
                }

                if (!foundValidSlot)
                {
                    Console.WriteLine($"无法为 {newDevice.Name} 的 {newDevice.WorkContent} 找到合适的时间分配。");

                    // 确保在 UI 线程上执行（如果这是一个WinForms应用程序）
                    if (Application.OpenForms.Count > 0)
                    {
                        Application.OpenForms[0].Invoke((MethodInvoker)delegate
                        {
                            ErrorForm logForm = ErrorForm.GetInstance();
                            if (!logForm.Visible)
                            {
                                logForm.Show();
                            }
                            logForm.AppendLog($"{DateTime.Now}:无法为 {newDevice.Name} 的 {newDevice.WorkContent} 找到合适的时间分配。");
                        });
                    }
                    else
                    {
                        Console.WriteLine("No open forms to attach the log window to.");
                    }
                }
            }

            return assignedNewDevices;
        }

        static bool HasConflict(List<ScheduleDevice> devices, string deviceName, DateTime startTime, DateTime endTime)
        {
            foreach (var device in devices)
            {
                if (device.Name == deviceName && IsOverlap(device.StartTime, device.EndTime, startTime, endTime))
                {
                    return true;
                }
            }
            return false;
        }

        static bool IsOverlap(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
        {
            return (start1 < end2 && end1 > start2);
        }
    }
}
